﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRT;

public partial class client : System.Web.UI.Page
{
    AnsweringServiceDataContext db = new AnsweringServiceDataContext();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                ViewState["ClientID"] = Convert.ToInt16(Request.QueryString["ID"]);
                PopulateItems((short)ViewState["ClientID"]);
                DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/clients.aspx");
                //string closeScript = "alert('"+ex.Message+"')";
                //if (!Page.ClientScript.IsStartupScriptRegistered("clientScript"))
                //    Page.ClientScript.RegisterStartupScript(Page.GetType(), "clientScript", closeScript);
            }
        }
    }

    protected void PopulateItems(short ClientID)
    {
        Client client = db.Clients.Single(c => c.ClientID == ClientID);
        lbClient.Text = client.Name + "(" + client.Number + ")";
        if (client.Scratchpad != null)
        {
            lbScratchpad.Text = client.Scratchpad.Replace("\r\n", "<br />");
            tbEditScratchpad.Text = client.Scratchpad;
        }
        if (client.Instructions != null)
            lbInstructions.Text = client.Instructions.Replace("\r\n", "<br />");
    }

    protected void btUpdateScratchpad_Click(object sender, EventArgs e)
    {
        var client = (from c in db.Clients
                      where c.ClientID == (short)ViewState["ClientID"]
                      select c).First();
        client.Scratchpad = tbEditScratchpad.Text;
        db.SubmitChanges();
        lbScratchpad.Text = tbEditScratchpad.Text.Replace("\r\n", "<br />");
    }

    protected void ddSpecificInstructions_DataBinding(object sender, EventArgs e)
    {
        var specInstructions = from si in db.SpecificInstructions
                               where si.ClientID == (short)ViewState["ClientID"]
                               select si;
        ddSpecificInstructions.DataTextField = "Description";
        ddSpecificInstructions.DataValueField = "SpecificInstructionID";
        ddSpecificInstructions.DataSource = specInstructions;
        if (specInstructions.Count() > 0)
            lbSpecificInstructions.Text = specInstructions.First().Instructions.Replace("\r\n", "<br />");
        else
            ddSpecificInstructions.Visible = false;
    }

    protected void ddSpecificInstructions_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbSpecificInstructions.Text = db.SpecificInstructions.Single(i => i.SpecificInstructionID == Convert.ToInt32(ddSpecificInstructions.SelectedValue)).Instructions.Replace("\r\n","<br />");
    }
    
    protected void lvMessages_DataBinding(object sender, EventArgs e)
    {
        var messages = from m in db.Messages
                       where m.Call.Event.ClientID == (short)ViewState["ClientID"]
                       select m;
        lvMessages.DataSource = messages;
    }

    protected void lvRecentCalls_DataBinding(object sender, EventArgs e)
    {
        var calls = from c in db.Calls
                    where c.Event.ClientID == (short)ViewState["ClientID"]
                    select c;
        lvRecentCalls.DataSource = calls;
    }
}
